﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Orders;
using System.Data.SqlClient;
using System.Web.Caching;
using System.Web.Configuration;
using System.Diagnostics;

public partial class Orders_ViewOrders : System.Web.UI.Page
{
    #region Properties

    protected List<ContactData> ContactPersons { get; set; }
    protected List<EmployeeData> Employees { get; set; }
    protected List<PaymentTerm> PaymentTerms { get; set; }
    protected List<DeliveryTerm> DeliveryTerms { get; set; }
    protected List<Currency> Currencies { get; set; }
    protected List<FreightTerm> FreightTerms { get; set; }
    protected List<DeliveryMethod> DeliveryMethods { get; set; }
    public int OrderId
    {
        get { return mOrderId; }
        set { mOrderId = value; }
    }

    #endregion

    #region Fields

    private int mOrderId = -1;

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        btnViewProducts.Enabled = false;
    }

    protected void InitHelperCollections()
    {
        OrdersDataClassesDataContext dc = new OrdersDataClassesDataContext();
        ContactPersons = (from c in dc.ContactPersons
                          select new ContactData()
                          {
                              ContactPersonID = c.ContactPersonID,
                              ContactName = c.FirstName + " " + c.LastName
                          }).ToList();
        Employees = (from e in dc.Employees
                     select new EmployeeData()
                     {
                         EmployeeId = e.EmployeeID,
                         EmployeeName = string.Join(" ", new string[] { e.FirstName, e.MiddleName, e.LastName })
                     }).ToList();

        DeliveryTerms = dc.DeliveryTerms.LinqCache(dc, "DeliveryTerms");
        Currencies = dc.Currencies.LinqCache(dc, "Currencies");
        PaymentTerms = dc.PaymentTerms.LinqCache(dc, "PaymentTerms");
        FreightTerms = dc.FreightTerms.LinqCache(dc, "FreightTerms");
        DeliveryMethods = dc.DeliveryMethods.LinqCache(dc, "DeliveryMethods");
    }

   
    protected struct EmployeeData
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
    }

    protected struct ContactData
    {
        public int ContactPersonID { get; set; }
        public string ContactName { get; set; }
    }

    protected void ldsPurchaseOrderDetails_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        foreach (KeyValuePair<string, object> kvp in e.WhereParameters)
        {
            if (kvp.Value == null) { e.Cancel = true; return; }
        }
    }

    protected void gvOrderList_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnViewProducts.Enabled = true;
        dvOrderInfo.ChangeMode(DetailsViewMode.ReadOnly);
    }
    protected void dvOrderInfo_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        if (e.NewMode == DetailsViewMode.Edit ||
            e.NewMode == DetailsViewMode.Insert)
        {
            InitHelperCollections();
        }
        if (e.CancelingEdit == true && gvOrderList.SelectedIndex > -1)
        {
            btnViewProducts.Enabled = true;
        }
    }
    protected void dvOrderInfo_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        gvOrderList.DataBind();
    }
    protected void dvOrderInfo_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        gvOrderList.DataBind();
    }

    protected void btnNewOrder_Click(object sender, EventArgs e)
    {
        gvOrderList.SelectedIndex = -1;
        InitHelperCollections();
        dvOrderInfo.ChangeMode(DetailsViewMode.Insert);
    }
    protected void btnViewProducts_Click(object sender, EventArgs e)
    {
        object selectedOrderId = gvOrderList.SelectedDataKey.Value;
        if (selectedOrderId != null)
        {
            string productsUrl = "~/Documents/PurchaseOrder/PurchaseOrderItems.aspx?OrderId="
                + selectedOrderId.ToString();
            Response.Redirect(productsUrl);
        }
    }
}
